package com.shanpin.shebao.admin.module.shebao;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shanpin.core.utils.JsonUtil;
import com.shanpin.shebao.admin.module.shebao.model.ConfigLog;

import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;
import de.danielbechler.diff.node.Visit;

@Service
public class SBConfigLogService {
	
	private static Logger log = LoggerFactory.getLogger(SBConfigLogService.class);
	
	@Autowired
	private SBConfigLogDao configLogDao;

	private final static Map<String, String> map = new HashMap<>();
	private final static Map<String, String> mapItem = new HashMap<>();
	static {
		map.put("shebao_type", "档位名称");
		map.put("proxy_type", "社保操作规则");
		map.put("fund_opt", "公积金操作规则");
		map.put("shebao_charge", "社保服务费");
		map.put("fund_charge", "公积金服务费");
		map.put("package_charge", "打包服务费");
		map.put("inc_date", "增员截止日期");
		map.put("dec_date", "减员截止日期");
		map.put("shebao_desc", "社保购买提示");
		map.put("fund_desc", "公积金购买提示");
		map.put("pension", "养老保险");
		map.put("lose_job", "失业保险");
		map.put("injury", "工伤保险");
		map.put("birth", "生育保险");
		map.put("medical", "基本医疗");
		map.put("hospital", "住院医疗");
		map.put("major", "重大疾病");
		map.put("large_major", "大病（额）医疗");
		map.put("village", "农民工医疗");
		map.put("education", "教育统筹经费");
		map.put("warm", "采暖费");
		map.put("archives", "档案费");
		map.put("added", "补充医疗");
		map.put("big_major", "大病（额）医疗-月年缴");
		map.put("supplement", "补充医疗-月年缴");
		map.put("disabled", "残保金");
		map.put("fund", "公积金");
		map.put("update_time", "更新时间");
		
		mapItem.put("{min}", "最低");
		mapItem.put("{max}", "最高");
		mapItem.put("{e}", "企业比例");
		mapItem.put("{p}", "个人比例");
	}
	
	private String getName(String nodeName) {
		StringBuilder b = new StringBuilder();
		for (Entry<String, String> entry : map.entrySet()) {
			if (nodeName.indexOf(entry.getKey()) > -1) {
				b.append(entry.getValue() + ".");
				break;
			}
		}
		for (Entry<String, String> entry : mapItem.entrySet()) {
			if (nodeName.indexOf(entry.getKey()) > -1) {
				b.append(entry.getValue());
				break;
			}
		}
		return b.toString();
	}

	public List<ConfigLog> logList() throws IOException {
		List<ConfigLog> logs = configLogDao.logList();
		for (ConfigLog clog : logs) {
			final StringBuilder msgBuilder = new StringBuilder(32);
			if (StringUtils.isBlank(clog.getPre_content())) {
				continue;
			}
			msgBuilder.append("id: " + clog.getCid() + "<br>");
			final Map<String, String> pre = (Map<String, String>) JsonUtil.string2Map(clog.getPre_content());
			final Map<String, String> curr = (Map<String, String>) JsonUtil.string2Map(clog.getCurr_content());
			DiffNode diff = ObjectDifferBuilder.buildDefault().compare(pre, curr);
			diff.visit(new DiffNode.Visitor() {
			    public void node(DiffNode node, Visit visit) {
			        Object baseValue = node.canonicalGet(pre);
			        Object workingValue = node.canonicalGet(curr);
			        if (workingValue != null && workingValue.toString().indexOf("{") > -1) {
			        	return;
			        }
			        msgBuilder.append(getName(node.getPath().toString()) + " ： " + 
			                               baseValue + " ---> " + workingValue + "<br>");
			    }
			});
			clog.setCurr_content(msgBuilder.toString());
		}
		return logs;
	}
	
}
